//
//  NewSetErweiMaViewController.m
//  wangzhe
//
//  Created by 李 佳伟 on 2018/3/29.
//  Copyright © 2018年 李 佳伟. All rights reserved.
//

#import "NewSetErweiMaViewController.h"
#import "JTCSM.h"
#import "STAlbumManager.h"
static CGFloat erweimaImagesize = 140;
@interface NewSetErweiMaViewController ()
{
    UIImageView *backImageView;
    UIImageView *erweimaImageView;
    UIImageView *alertImageView;
    UIImageView *imgvCtrl;
    UIView *shareView;
    BOOL isShowAlert;
    
}

@end

@implementation NewSetErweiMaViewController

- (void)gotBack{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [df isRemoveTarbar:YES];
    self.navigationController.navigationBar.hidden = NO;
    self.view.backgroundColor =[UIColor whiteColor];
    leftBarButtonItems_rightBarButtonItems *leftbtn = KCUSTOMNAVIGSTION_ALLOC;
    self.navigationItem.leftBarButtonItem = [leftbtn setBarButtonItemsImageName:@"fanhui.png" frame:KLeftBarButtonItemFrame_image target:self SEL:@selector(gotBack)];
    // Do any additional setup after loading the view.
    [self layoutSubView];
    [self layoutShareView];
}

- (void)layoutSubView{
//    1.4921
    
    CGFloat space = 35;
    CGFloat imageWidth = KCONTROLLERWith-2*space;
    backImageView = [[UIImageView alloc]initWithFrame:CGRectMake(space, space, imageWidth, imageWidth*1.4921)];
    backImageView.image = [UIImage imageNamed:@"制作二维码-大图.png"];
    backImageView.userInteractionEnabled = YES;
    [self.view addSubview:backImageView];
    
    erweimaImageView = [[UIImageView alloc]initWithFrame:CGRectMake((backImageView.LC_width-erweimaImagesize)/2, 70, erweimaImagesize, erweimaImagesize)];
    erweimaImageView.image = [UIImage imageNamed:@"erweima.jpg"];
    [backImageView addSubview:erweimaImageView];
    erweimaImageView.userInteractionEnabled = YES;
    UIPanGestureRecognizer *gesture1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(onDrag:)];
    [erweimaImageView addGestureRecognizer:gesture1];
    
    imgvCtrl = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    imgvCtrl.center = [erweimaImageView convertPoint:CGPointMake(erweimaImagesize, 0) toView:backImageView];
    imgvCtrl.image = [UIImage imageNamed:@"拉伸.png"];
    imgvCtrl.userInteractionEnabled = YES;
    [backImageView addSubview:imgvCtrl];
    UIPanGestureRecognizer *gesture5 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(onScaleAndRotate:)];
    [imgvCtrl addGestureRecognizer:gesture5];
    
    
    UIButton *tijiaobutton = [UIButton buttonWithType:UIButtonTypeCustom];
    tijiaobutton.frame = CGRectMake(25, KVIEW_Y(backImageView)+30, KCONTROLLERWith-40, 50);
    [tijiaobutton addTarget:self action:@selector(tijiao) forControlEvents:UIControlEventTouchUpInside];
    [df layerWithView:tijiaobutton radius:5 width:0 color:nil];
    tijiaobutton.backgroundColor = KZhuTiRGB;
    [tijiaobutton setTitleColor:RGBACOLOR(51, 51, 51, 1) forState:UIControlStateNormal];
    [tijiaobutton setTitle:@"制作二维码" forState:UIControlStateNormal];
    [self.view addSubview:tijiaobutton];
    
    alertImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWidth, KSCREENHeight)];
    alertImageView.image = [UIImage imageNamed:@"提示1.png"];
    [[JTCSMSingle shareSingle].theWindow addSubview:alertImageView];
    alertImageView.userInteractionEnabled = YES;
    [df addTapGestureRecognizerWithTarget:self action:@selector(changeImage:) andaddView:alertImageView];
    isShowAlert = YES;
    
}
- (void)changeImage:(UITapGestureRecognizer *)tap{
    if (isShowAlert) {
        isShowAlert = NO;
        alertImageView.image = [UIImage imageNamed:@"提示2.png"];
    }else{
        [alertImageView removeFromSuperview];
    }
}

-(void)onDrag:(UIPanGestureRecognizer*)gesture
{
    
    CGPoint translation = [gesture translationInView:backImageView];
    CGPoint center = gesture.view.center;
    center.x += translation.x;
    center.y += translation.y;
    gesture.view.center = center;
    [gesture setTranslation:CGPointZero inView:backImageView];
    imgvCtrl.center = [erweimaImageView convertPoint:CGPointMake(erweimaImagesize, 0) toView:backImageView];

}


-(void)onScaleAndRotate:(UIPanGestureRecognizer*)gesture
{
    UIView *viewCtrl = gesture.view;
    UIView *viewImg = erweimaImageView;
    CGPoint center = viewImg.center;
    CGPoint prePoint = viewCtrl.center;
    CGPoint translation = [gesture translationInView:gesture.view];
    CGPoint curPoint = CGPointMake(prePoint.x+translation.x, prePoint.y+translation.y);
    // 计算缩放
    CGFloat preDistance = [self getDistance:prePoint withPointB:center];
    
    
    CGFloat curDistance = [self getDistance:curPoint withPointB:center];

    CGFloat scale = curDistance / preDistance;
    
     NSLog(@"放大%f  缩小%f  比例%f",preDistance,curDistance,backImageView.LC_width);
    
    // 计算弧度
    CGFloat preRadius = [self getRadius:center withPointB:prePoint];
    CGFloat curRadius = [self getRadius:center withPointB:curPoint];
    CGFloat radius = curRadius - preRadius;
    radius = - radius;
    
    CGAffineTransform transform = CGAffineTransformScale(viewImg.transform, scale, scale);
    viewImg.transform = CGAffineTransformRotate(transform, radius);
    viewCtrl.center = [viewImg convertPoint:CGPointMake(erweimaImagesize, 0) toView:backImageView];
    [gesture setTranslation:CGPointZero inView:viewCtrl];
    
}
-(CGFloat)getDistance:(CGPoint)pointA withPointB:(CGPoint)pointB
{
    CGFloat x = pointA.x - pointB.x;
    CGFloat y = pointA.y - pointB.y;
    return sqrt(x*x + y*y);
}
-(CGFloat)getRadius:(CGPoint)pointA withPointB:(CGPoint)pointB
{
    CGFloat x = pointA.x - pointB.x;
    CGFloat y = pointA.y - pointB.y;
    return atan2(x, y);
}


-(UIImage *)addImageLogo:(UIImage *)image logoView:(UIImageView *)logoImageV
{
    
    //原始图片的宽和高，可以根据需求自己定义
    CGFloat w = backImageView.LC_width;
    CGFloat h = backImageView.LC_height;
    CGFloat changeBili = 1.5;
    //logo的宽和高和位置
    CGRect logoRect = logoImageV.frame;
    //以1.png的图大小为画布创建上下文
    UIGraphicsBeginImageContext(CGSizeMake(w*changeBili, h*changeBili));
    [image drawInRect:CGRectMake(0, 0, w*changeBili, h*changeBili)];
    [logoImageV.image drawInRect:CGRectMake(logoRect.origin.x*changeBili, logoRect.origin.y*changeBili, logoRect.size.width*changeBili, logoRect.size.height*changeBili)];//再把小图放在上下文中
    
    UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();//从当前上下文中获得最终图片
    UIGraphicsEndImageContext();//关闭上下文
    
    return resultImg;
}

- (void)tijiao{
    imgvCtrl.hidden = YES;
    UIImage *newImage = [self imageFromView:backImageView];
    [[STAlbumManager sharedManager] saveImage:newImage toAlbum:nil completionHandler:^(UIImage *image, NSError *error) {
        imgvCtrl.hidden = NO;
    }];
    
    [self showview];
}

#pragma mark - UIView转化为UIImage
- (UIImage *)imageFromView:(UIView *)view
{
    CGFloat scale = [UIScreen mainScreen].scale;
    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)layoutShareView{
    shareView = [[UIView alloc]initWithFrame:CGRectMake(0, KCONTROLLERHeight, KCONTROLLERWith, 220)];
    shareView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:shareView];
    
    CGFloat space = 60;
    CGFloat top = 20;
    CGFloat titleIamgeWidth = shareView.LC_width-space*2;
    CGFloat titleImageHeight = titleIamgeWidth*0.0551524;
    UIImageView *titleImageV = [[UIImageView alloc]initWithFrame:CGRectMake(space, top, titleIamgeWidth, titleImageHeight)];
    titleImageV.image = [UIImage imageNamed:@"选择分享方式.png"];
    [shareView addSubview:titleImageV];
    
    UIImageView *view;
    NSArray *imageArray = @[@"微信-.png",@"朋友圈-.png",@"QQ-.png",@"空间-.png"];
    CGFloat spaceofLeft = 50;
    CGFloat typeviewWidth = (KCONTROLLERWith-5*spaceofLeft)/4;
    CGFloat typeviewHeight = typeviewWidth*1.4666666;
    for (int i = 0 ; i<imageArray.count; i++) {
        view= [[UIImageView alloc]initWithFrame:CGRectMake(spaceofLeft+(typeviewWidth+spaceofLeft)*i,KVIEW_Y(titleImageV)+top, typeviewWidth, typeviewHeight)];
        view.image = [UIImage imageNamed:imageArray[i]];
        view.tag = 2000+i;
        view.userInteractionEnabled = YES;
        [df addTapGestureRecognizerWithTarget:self action:@selector(gotoVC:) andaddView:view];
        [shareView addSubview:view];
    }
    [df newlineViewWithHeightofTop:KVIEW_Y(view)+20 superView:shareView spacing:0 color:RGBACOLOR(241, 241, 241, 1) lineHeight:1];
    
    UILabel *buttonLabel = [UILabel initWithFrame:CGRectMake(0, KVIEW_Y(view)+20, KCONTROLLERWith, 50) text:@"取消" font:16 textAlignment:NSTextAlignmentCenter texcolor:[df colorWithHexString:@"333333" alpha:1] backgroundColor:nil];
    [shareView addSubview:buttonLabel];
    shareView.userInteractionEnabled = YES;
    buttonLabel.userInteractionEnabled = YES;
    [df addTapGestureRecognizerWithTarget:self action:@selector(removeview) andaddView:buttonLabel];
    
     shareView.frame = CGRectMake(0, KCONTROLLERHeight, KCONTROLLERWith, KVIEW_Y(buttonLabel));
    [df setTheViewYiYing:shareView shadowColor:RGBACOLOR(222, 222, 222, 1) shadowOffset:CGSizeMake(3, 3) apla:0.8];
    
    
}
- (void)removeview{
    [UIView animateWithDuration:0.3 animations:^{
        shareView.frame  = CGRectMake(0, KCONTROLLERHeight, KCONTROLLERWith, shareView.LC_height);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)showview{
    [UIView animateWithDuration:0.3 animations:^{
        shareView.frame  = CGRectMake(0, KCONTROLLERHeight-shareView.LC_height, KCONTROLLERWith, shareView.LC_height);
    } completion:^(BOOL finished) {
        
    }];
}
- (void)gotoVC:(UITapGestureRecognizer *)tap{
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
